package app.gui;

//import org.apache.log4j.Logger;

import color.Color;
import processing.core.PApplet;
import processing.core.PFont;

public class NumberScroller implements Interactive{
	/**
	 * Logger for this class
	 */
	//private static final Logger logger = Logger.getLogger(NumberScroller.class);

	PFont font;
	int size;
	int x, y;
	Color color;
	public float val;
	float minVal;
	float range;
	private boolean isDragging;
	private int sX, sY;
	private float sVal;
	private float yScale = 0.25f;

	
	public NumberScroller(PApplet pa, int size, int x, int y, Color color, float minVal, float range) {
		super();
		font = pa.loadFont("FedraSans-Book-24.vlw");
		this.size = size;
		this.x = x;
		this.y = y;
		this.color = color;
		this.minVal = minVal;
		this.range = range;
	}

	public void draw(PApplet pa){
		Color.oppositeBrightness(Color.complementary(color)).fill(pa);
		pa.rect(x, y, size * 2, size);
		pa.textFont(font, size / 2f);
		color.fill(pa);
		pa.text(""+val, x + size / 12f, y + size * 2f / 3f);
	}

	public void mousePressed(PApplet pa){
		if(!contains(pa.mouseX, pa.mouseY))
			return;

//		if (logger.isDebugEnabled()) {
//			logger.debug("mousePressed(PApplet) - mouse pressed ");
//		}

		if(!isDragging){
			sX = pa.mouseX;
			sY = pa.mouseY;
			sVal = val;
			isDragging = true;
		}
	}
	
	public void mouseReleased(PApplet pa){
		
		isDragging = false;
	}
	

	public void mouseDragged(PApplet pa){
//		if (logger.isDebugEnabled()) {
//			logger.debug("mouseDragged(PApplet) - mouse dragging" + isDragging);
//		}

		if(isDragging){
			val = sVal - (pa.mouseY - sY) * yScale;
			val = Math.min(Math.max(val, minVal), minVal + range);
		}
		
	}
	public boolean contains(float x, float y){
		if(x < this.x || x > this.x + size) return false;
		if(y < this.y || x > this.y + size) return false;
		return true;
	}

	public void keyPressed(PApplet pa) {
		// TODO Auto-generated method stub
		
	}

	public void mouseMoved(PApplet pa) {
		// TODO Auto-generated method stub
		
	}

}
